package com.luck.easyfoodbatch.pickercity;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;

import com.baidu.platform.comapi.map.E;
import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.db.FindDB;
import com.luck.easyfoodbatch.entity.City;
import com.luck.easyfoodbatch.pickerview.LoopListener;
import java.util.ArrayList;
import java.util.List;

public class PopCityHelper {

    private Context context;
    private PopupWindow pop;
    private View view;
    private OnClickOkListener onClickOkListener;

    private List<City> province;
    private List<City> city;
    private List<City> area;
    private String p, c, a, pid, cid, aid;
    private FindDB findDB;

    public PopCityHelper(Context context) {
        this.context = context;
        view = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.picker_city, null);
        pop = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        initPop();
        initData();
        initView();
    }


    private void initPop() {
        pop.setAnimationStyle(android.R.style.Animation_InputMethod);
        pop.setFocusable(true);
        pop.setOutsideTouchable(true);
        pop.setBackgroundDrawable(new BitmapDrawable());
        pop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    }


    /**
     * 初始化数据
     */
    private String id = "";

    private void initData() {
        findDB = new FindDB(context);
        province = findDB.getProvinces();// 省
        if (province.size() > 0) {
            id = province.get(0).getId();
            city = findDB.getCitys(id); // 市
        } else {
            city = new ArrayList<>();
        }
        if (city.size() > 0) {
            area = findDB.getAreas(id);// 区
        } else {
            area = new ArrayList<>();
        }

    }

    /**
     * 第一次加载
     */
    private boolean isLoading1 = false;
    private boolean isLoading2 = false;
    private boolean isLoading3 = false;

    private void initView() {
        Button btnCancel = (Button) view.findViewById(R.id.btnCancel);
        Button btnOk = (Button) view.findViewById(R.id.btnOK);
        final LoopCityView loopView1 = (LoopCityView) view.findViewById(R.id.loopView1);
        final LoopCityView loopView2 = (LoopCityView) view.findViewById(R.id.loopView2);
        final LoopCityView loopView3 = (LoopCityView) view.findViewById(R.id.loopView3);
        loopView1.setList(province);
        loopView1.setNotLoop();
        loopView1.setCurrentItem(0);

        loopView2.setList(city);
        loopView2.setNotLoop();
        loopView2.setCurrentItem(0);

        loopView3.setList(area);
        loopView3.setNotLoop();
        loopView3.setCurrentItem(0);
        loopView1.setListener(new LoopListener() {
            @Override
            public void onItemSelect(int item) {
                if (!isLoading1) {
                    id = province.get(0).getId();
                    city = findDB.getCitys(id); // 市
                    area = findDB.getAreas(city.get(0).getId());// 区
                    pid = id;
                    p = province.get(0).getName();
                } else {
                    id = province.get(item).getId();
                    city = findDB.getCitys(id); // 市
                    area = findDB.getAreas(city.get(0).getId());// 区
                    pid = id;
                    p = province.get(item).getName();
                }
                loopView2.setList(city);
                loopView2.setNotLoop();
                loopView2.setCurrentItem(0);

                loopView3.setList(area);
                loopView3.setNotLoop();
                loopView3.setCurrentItem(0);

                isLoading1 = true;
            }
        });
        loopView2.setListener(new LoopListener() {
            @Override
            public void onItemSelect(int item) {
                if (!isLoading2) {
                    c = city.get(0).getName();
                    cid = city.get(0).getId();
                } else {
                    c = city.get(item).getName();
                    cid = city.get(item).getId();
                }
                isLoading2 = true;

                area = findDB.getAreas(city.get(item).getId());// 区
                loopView3.setList(area);
                loopView3.setNotLoop();
                loopView3.setCurrentItem(0);
            }
        });
        loopView3.setListener(new LoopListener() {
            @Override
            public void onItemSelect(int item) {
                if (!isLoading3) {
                    a = area.get(0).getName();
                    aid = area.get(0).getId();
                } else {
                    try {
                        a = area.get(item).getName();
                        aid = area.get(item).getId();
                    }catch (Exception e){}

                }
                isLoading3 = true;
            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop.dismiss();
            }
        });

        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        onClickOkListener.onClickOk(p + "-" + c + "-" + a, pid, cid, aid);
                        pop.dismiss();
                    }
                }, 100);
            }
        });
    }

    /**
     * 显示
     *
     * @param view
     */
    public void show(View view) {
        pop.showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }

    /**
     * 隐藏监听
     *
     * @param onDismissListener
     */
    public void setOnDismissListener(PopupWindow.OnDismissListener onDismissListener) {
        pop.setOnDismissListener(onDismissListener);
    }

    public void setOnClickOkListener(OnClickOkListener onClickOkListener) {
        this.onClickOkListener = onClickOkListener;
    }

    public interface OnClickOkListener {
        public void onClickOk(String birthday, String pid, String cid, String aid);
    }

}
